package com.haishen.xiagent.demo.invoke.affiliates;

// Copyright (c) Alibaba, Inc. and its affiliates.

import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisListResult;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.task.AsyncTaskListParam;
import com.alibaba.dashscope.utils.Constants;
import com.alibaba.dashscope.utils.JsonUtils;
import com.haishen.xiagent.demo.invoke.TestApiKey;

import java.util.HashMap;
import java.util.Map;

/**
 * Alibaba 图像生成
 */
public class GenerateImages {
    static {
        // 以下为北京地域url，若使用新加坡地域的模型，需将url替换为：https://dashscope-intl.aliyuncs.com/api/v1
        Constants.baseHttpApiUrl = "https://dashscope.aliyuncs.com/api/v1";
    }

    // 若没有配置环境变量，请用百炼API Key将下行替换为：apiKey="sk-xxx"
    // 新加坡和北京地域的API Key不同。获取API Key：https://help.aliyun.com/zh/model-studio/get-api-key
    static String apiKey = TestApiKey.API_KEY;

    public static void basicCall() throws ApiException, NoApiKeyException {
        String prompt = "一幅采用尾田荣一郎《海贼王》标志性风格创作的动感漫画分镜，主角为蒙奇・D・路飞的五档形态。路飞标志性的草帽向后倾斜，狂放的白发在风中极具张力地飘动，脸上洋溢着充满活力的灿烂笑容。他正处于战斗动作之中 —— 右臂伸展成巨大的橡胶拳头，周围环绕着闪耀的红白两色霸气，朝着一群身穿破旧黑大衣、衣衫褴褛的海盗杂兵猛击而去。\n" +
                "背景呈现出电闪雷鸣下波涛汹涌的狂暴海面。远处，一艘残破的海盗船损毁不堪，船上的骷髅旗残破不全，在风中剧烈飘动。明亮的闪电划破乌云，进一步烘托出紧张激烈的戏剧氛围。\n" +
                "画风严格遵循尾田的标志性美学：粗重的黑色墨线轮廓、鲜明的平涂色彩（深邃的蓝色大海、明亮的金色草帽、温暖的橙色背心）、夸张的面部表情，以及突出动作与冲击力的强劲动态线条。画面焦点完全集中在路飞这位核心角色身上 —— 衣物纹理细节清晰（背心上的褶皱、草帽边缘的磨损痕迹），双眼炯炯有神、闪烁着坚定与喜悦的光芒。\n" +
                "这是一幅为《海贼王》漫画单行本封面设计的关键战斗场景，散发着冒险气息、紧张感与热血精神。画面简洁无多余元素，视觉冲击力拉满。";
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("prompt_extend", false);
        parameters.put("watermark", true);
        ImageSynthesisParam param =
                ImageSynthesisParam.builder()
                        .apiKey(apiKey)
                        .model("qwen-image-plus")
                        .prompt(prompt)
                        .n(1)
                        .size("1328*1328")
                        .parameters(parameters)
                        .build();

        ImageSynthesis imageSynthesis = new ImageSynthesis();
        ImageSynthesisResult result = null;
        try {
            System.out.println("---同步调用，请等待任务执行----");
            result = imageSynthesis.call(param);
        } catch (ApiException | NoApiKeyException e){
            throw new RuntimeException(e.getMessage());
        }
        System.out.println(JsonUtils.toJson(result));
    }

    //获取任务列表
    public static void listTask() throws ApiException, NoApiKeyException {
        ImageSynthesis is = new ImageSynthesis();
        AsyncTaskListParam param = AsyncTaskListParam.builder().build();
        ImageSynthesisListResult result = is.list(param);
        System.out.println(result);
    }

    //根据taskId获取任务结果
    public void fetchTask() throws ApiException, NoApiKeyException {
        String taskId = "your task id";
        ImageSynthesis is = new ImageSynthesis();
        // 如果设置了 DASHSCOPE_API_KEY 环境变量，apiKey 可以为 null
        ImageSynthesisResult result = is.fetch(taskId, null);
        System.out.println(result.getOutput());
        System.out.println(result.getUsage());
    }

    public static void main(String[] args){
        try{
            basicCall();
            //listTask();
        }catch(ApiException|NoApiKeyException e){
            System.out.println(e.getMessage());
        }
    }
}
